﻿#region Declaration

//------------------------------------------------------------------------------
// Compas Information Techonoly Co., Ltd
// The Initial Developer of the Original Code is CompasSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

#endregion

#region Using Directives

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

#endregion

namespace QuickDev.Common
{
    /// <summary>
    /// Memberships
    /// </summary>
    public class Memberships
    {
        /// <summary>
        /// Current UserName
        /// </summary>
        /// <returns></returns>
        public static string CurrentUserName
        {
            get
            {
                if ( HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated )
                {
                    string[] userIdAndName = HttpContext.Current.User.Identity.Name.Split( ',' );
                    if ( userIdAndName.Length > 1 )
                    {
                        return userIdAndName[1];
                    }
                }
                return String.Empty;
            }
        }

        /// <summary>
        /// Current UserId
        /// </summary>
        /// <returns></returns>
        public static int CurrentUserId
        {
            get
            {
                if ( HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated )
                {
                    string[] userIdAndName = HttpContext.Current.User.Identity.Name.Split( ',' );

                    int userId = 0;
                    int.TryParse( userIdAndName[0] , out userId );

                    return userId;
                }

                return 0;
            }
        }

        /// <summary>
        /// IsInGroup
        /// </summary>
        /// <returns>bool</returns>
        public static bool IsInGroup( string role )
        {
            return HttpContext.Current.User.IsInRole( role );
        }

        /// <summary>
        /// IsAuthenticated
        /// </summary>
        /// <returns></returns>
        public static bool IsAuthenticated
        {
            get
            {
                return HttpContext.Current.User.Identity.IsAuthenticated;
            }
        }
    }
}
